<div class="modal-content" xmlns:th="http://www.w3.org/1999/xhtml">
    <!--/* 加载数据表格样式 */-->
    <!--<th:block th:replace="decorator::datagridStyle"></th:block>-->
    <!--<th:block th:replace="decorator::treeStyle"></th:block>-->
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
        <h4 class="modal-title">选择用户</h4>
    </div>
    <div class="modal-body">
        <table id="table" style="width: 100%;"></table>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn blue save">保存</button>
    </div>
</div>
<div xmlns:th="http://www.w3.org/1999/xhtml">
    <style>
        td[field="checkbox"] input[type="checkbox"] {
            /*margin-right: 10px;*/
            position: relative;
        }

        .columnbtn_background img {
            width: 16px;
        }

        td div {
            word-break: keep-all !important;
            overflow: hidden !important;
            white-space: nowrap !important;
            text-overflow: ellipsis !important;
            color: #2F353B;
        }

        .datagrid-filter-c input[type='text']:first-child {
            width: 90% !important;
            height: 80% !important;
            left: 5%;
            top: 10%;
        }

        .datagrid-filter-c .textbox {
            width: 90% !important;
            height: 26px !important;
            left: 5%;
            top: 10%;
        }

        .datagrid-filter-c {
            height: 100%;
        }

        .datagrid-filter-c .textbox span a {
            width: 22px;
            height: 23px;
        }
    </style>
    <script th:inline="javascript">
        var modalId;
        var checkData = {};
        $(function () {
            var dialog = dialogs[0];
            modalId = dialog.attr("id");
            var singleSelect = dialog.params.singleSelect;
            if (singleSelect == undefined || singleSelect == null || singleSelect == "") {
                singleSelect = false;
            }
            if (dialog.params.multiSelect !== undefined) {
                singleSelect = !dialog.params.multiSelect;
            }
            var resultData = [];
            $("#searchFormDialog").on("keyup", "input", function (ev) {
                if (ev.keyCode == 13) {
                    $('#searchFormDialog').find("a:contains('查询')")[0].click();
                }
            });
            var groupUsers = [[${groupUsers}]];
            dialog.success = function () {
                $(dialog).width(900);
                // setWhere(dialog);
                var height = $("#" + modalId + " #table").parent().height();
                // console.log(height);
                //角色Id
                var roleIds =/*[[${param.roleIds}]]*/
                //角色编号，多个角色编号用,隔开
                var roleNos = dialog.params.roleNos;
                var excludeRoleIdListStr = dialog.params.excludeRoleIdListStr;
                var orgId =/*[[${param.orgId}]]*/
                var orgLevelCode =/*[[${param.orgLevelCode}]]*/
                var filterAll =/*[[${param.filterAll}]]*/;
                var isAuthorize =/*[[${param.authorize}]]*/;
                var loginname = /*[[${param.loginname}]]*/;
                var url = "/rbac/user/dataGrid?filterAll=" + (filterAll ? filterAll[0] : false) + "&authorize=" + (isAuthorize ? isAuthorize[0] : false);
                if (roleIds) {
                    url += "&roleIds=" + roleIds[0];
                }
                if (roleNos) {
                    url += "&roleNos=" + roleNos;
                }
                if (orgId) {
                    url += "&orgId=" + orgId[0];
                }
                if (orgLevelCode) {
                    url += "&orgLevelCode=" + orgLevelCode[0];
                }
                if (loginname) {
                    url += "&loginname=" + loginname;
                }
                $("#" + modalId + " #table").datagrid({
                    width: "100%",
                    height: height,
                    queryParams:{
                        excludeRoleIdListStr:excludeRoleIdListStr
                    },
                    "url": url,
                    fitColumns: true,
                    striped: true,
                    remoteSort: true,
                    multipleSelect: true,
                    checkOnSelect: true,
                    singleSelect: singleSelect,
                    // sortName: "mid",
                    // sortOrder: "asc",
                    pagination: true,
                    pagePosition: "bottom",
                    pageList: [20, 50, 100],
                    pageSize: 20,
                    onLoadSuccess: function (data) {
                        var rows = $("#" + modalId + " #table").datagrid("getRows");
                        if (rows) {
                            if (dialog.params.userIds) {
                                var userIdsList = dialog.params.userIds;
                                for (var i in userIdsList) {
                                    var judge = false;
                                    rows.forEach(function (row, index) {
                                        if (userIdsList[i].mid == row.mid) {
                                            checkData["data_" + row.mid] = row;
                                            judge = true
                                        }
                                    });
                                    if (!judge) {
                                        checkData["data_" + userIdsList[i].mid] = userIdsList[i];
                                    }
                                };
                            }
                            rows.forEach(function (row, index) {
                                if (checkData["data_" + row.mid]) {
                                    $("#" + modalId + " #table").datagrid("checkRow", index);
                                }
                            });
                        };
                        if(groupUsers.length>0){
                            var rows = data.rows;
                            for(var i=0;i<rows.length;i++){
                                for(var j=0;j<groupUsers.length;j++){
                                    if(groupUsers[j].userId == rows[i].mid){
                                        $("#" + modalId + " #table").datagrid('selectRow',i);
                                    }
                                }
                            }
                        }
                    }, //选中
                    onCheck: function (rowIndex, rowData) {
                        checkData["data_" + rowData.mid] = rowData;
                    },
                    onUncheck: function (rowIndex, rowData) {
                        checkData["data_" + rowData.mid] = null;
                        if (dialog.params.userIds) {
                            for (var i in dialog.params.userIds) {
                                if (rowData.mid == dialog.params.userIds[i].mid) {
                                    dialog.params.userIds.splice(i, 1);
                                }
                            }
                        }
                    },
                    //全选
                    onCheckAll: function (rows) {
                        rows.forEach(function (row) {
                            checkData["data_" + row.mid] = row;
                        });
                    },
                    onUncheckAll: function (rows) {
                        rows.forEach(function (row) {
                            checkData["data_" + row.mid] = null;
                            if (dialog.params.userIds) {
                                for (var i in dialog.params.userIds) {
                                    if (row.mid == dialog.params.userIds[i].mid) {
                                        dialog.params.userIds.splice(i, 1);
                                    }
                                }
                            }
                        });
                    },
                    "columns": [
                        [
                            {"field": "checkbox", checkbox: true},

                            {field: 'No', title: '序号', align: 'center', width: 60, formatter: btn_NoColumn},

                            {"field": "userNo", title: '员工编号', width: 100, sortable: true},
                            {"field": "name", title: '姓名', width: 100, sortable: true},
                            {
                                "field": "orgLevelCode", title: '组织', width: 200, fixed: true,
                                formatter: function (data, row, index) {
                                    return row.orgNameStr;
                                }
                            },
                            {
                                "field": "roleId", title: '岗位', width: 150,
                                formatter: function (data, row, index) {
                                    return row.roleNameStr;
                                }
                            },
                            {"field": "mobile", title: '手机号', width: 100,},
                            {
                                "field": "sex", title: '性别', width: 60,
                                formatter: function (data, row, index) {
                                    return data == 1 ? "男" : "女";
                                }
                            },
                            // {"field": "jobtitle",title:'职称',width:100,},
                            // {"field": "entryTime",title:'工作年限',
                            //     formatter:function (data, row, index) {
                            //         var today = new Date();
                            //         var da = new Date(data);
                            //         return  today.getFullYear()-da.getFullYear()==0 ? 1 : today.getFullYear()-da.getFullYear();
                            //     }
                            // },
                            // {"field": "entryTime",title:'入职时间',width:100,
                            //     formatter:function (data, row, index) {
                            //         var da = new Date(data);
                            //         return da.getFullYear()+"-"+da.getMonth()+"-"+da.getDay();
                            //     }
                            // },
                            // {"field": "edu",title:'学历',width:100,
                            //     formatter:function (data, row, index) {
                            //         if(data==null) return "";
                            //         else  return Dic.showName([[${#dic.listAll('rbac.education')}]], data.educationType);
                            //     }
                            // },
                            // {"field": "major",title:'专业',width:100,
                            //     formatter:function (data, row, index) {
                            //         if(data==null) return "";
                            //         else  return data.major;
                            //     }
                            // },
                            // {"field": "onJob",title:'是否在职',width:100,
                            //     formatter:function (data, row, index) {
                            //         return Dic.showName([[${#dic.listAll('rbac.user.onjob')}]], data);
                            //     }
                            // },
                            // {"field": "onGuard",title:'是否在岗',width:100,
                            //     formatter:function (data, row, index) {
                            //         return Dic.showName([[${#dic.listAll('rbac.user.onguard')}]], data);
                            //     }
                            // },
                        ]
                    ],
                    filterBtnIconCls: 'icon-filter',
                    remoteFilter: true,
                    clientPaging: false
                });
                $("#" + modalId + " #table").datagrid('enableFilter', [
                    {"field": "userNo", type: 'textbox'},
                    {"field": "name", type: 'textbox'},
                    {
                        "field": "orgLevelCode", type: 'combotree',
                        options: {
                            // width:200,
                            // panelWidth:200,
                            valueField: 'levelCode',
                            textField: 'text',
                            url: "/rbac/org/tree?filterAll=" + (filterAll ? filterAll[0] : false),
                            onSelect: function (value) {
                                $("#" + modalId + " #table").datagrid("options").queryParams.orgLevelCode = value.levelCode;
                                $("#" + modalId + " #table").datagrid('doFilter');
                            }
                        }
                    },
                    {
                        "field": "roleId", type: 'combobox',
                        options: {
                            // width: 200,
                            // panelWidth: 200,
                            editable: false,
                            valueField: 'mid',
                            textField: 'name',
                            url: "/rbac/role/listAll",
                            loadFilter: function (data) {
                                data.unshift({
                                    mid: "",
                                    name: "所有"
                                });
                                return data;
                            },
                            onChange: function (value) {
                                // $('#table').datagrid('removeFilterRule', 'roleId');
                                $("#" + modalId + " #table").datagrid('addFilterRule', {
                                    field: 'roleId',
                                    op: 'equal',
                                    value: value
                                });
                                $("#" + modalId + " #table").datagrid('doFilter');
                            }
                        }
                    },
                    {"field": "No", type: 'label'},
                    {"field": "mobile", type: 'label'},
                    {"field": "sex", type: 'label'},
                    // {"field": "jobtitle",type:'label'},
                    // {"field": "entryTime",type:'label'},
                    {"field": "edu", type: 'label'},
                    {"field": "major", type: 'label'},
                    // {"field": "onJob",type:'label'},
                    // {"field": "onGuard",type:'label'},
                    // {field: "mid",type:'label'}
                ]);
                var resetBtn = '<div style="background-color: transparent;;\n' +
                    '    color: #337ab7;\n' +
                    '    font-size: 12px;\n' +
                    '    text-align: center;\n' +
                    '    width: 90%;\n' +
                    '    margin-left: 5%;\n' +
                    '    line-height: 1.6;" onclick="reset()">重置</div>';
                $('.datagrid-header-row td').eq(0).attr("rowspan", 1);
                $('.datagrid-filter-row td').eq(0).html(resetBtn);
                // $("#" + modalId + " #table").datagrid({
                //     "url": "/rbac/user/dataGrid?filterAll=" + [[${filterAll}]],
                //     checkbox: true,
                //     isBackfill: true,
                //     backfillData: [],
                //     dataTable: {
                //         "columns": [
                //             {"data": "name"},
                //             {"data": "userNo"},
                //             {"data" : "sex",
                //                 render:function (data, type, row, meta) {
                //                     return data=1?'男':'女';
                //                 }},
                //             {"data": "department"},
                //             {"data": "position"},
                //             {"data": "mobile"},
                //             {"data": "mobile"}
                //         ],
                //         "ordering": false,
                //         "order": [
                //             [1, "asc"]
                //         ],
                //         scrollY: height + "px",
                //         "scrollCollapse": "true",
                //         "paging": "false"
                //     },
                //     onSelect: function (node,index) {
                //         resultData.push(node);
                //     },
                //     onUnSelect: function (node,index) {
                //         for(var i = 0; i < resultData.length; i++){
                //             if(resultData[i].mid == node.mid){
                //                 resultData.splice(i,1);
                //                 break;
                //             }
                //         }
                //     },
                //     onLoad: function (data) {
                //         for(var i = 0; i < resultData.length; i++){
                //             for(var j = 0; j < data.length; j++){
                //                 if(resultData[i].mid == data[j].mid){
                //                     data[j].checked = true;
                //                     break;
                //                 }
                //             }
                //         }
                //         return data;
                //     }
                // });
                // App.post("/rbac/org/treeGrid",{filterAll: [[${filterAll}]]},function (result) {
                //     $("#" + modalId + " #orgTree").czTree({
                //         "data": result.rows
                //     });
                //     $("#" + modalId + " #orgTree").on("changed.jstree",function (e, data) {
                //         var orgs = $("#" + modalId + " #orgTree").czTree("getSelected");
                //         var levelCodes = "";
                //         for(var i = 0 ; i < orgs.length; i++){
                //             if(levelCodes){
                //                 levelCodes += ",";
                //             }
                //             levelCodes += orgs[i].levelCode;
                //         }
                //         $("#" + modalId + " #orgsStr").val(levelCodes);
                //         search();
                //     })
                // },function (msg) {
                //     console.error(msg);
                // });
//                $("#" + modalId + " #key").change(function () {
//                    search();
//                });
            };
            $("#" + modalId + " #checkAll").change(function () {
                if ($(this).is(":checked")) {
                    $("#" + modalId + " #table").datagrid("selectAll");
                } else {
                    $("#" + modalId + " #table").datagrid("unselectAll");
                }
            });
            $("#" + modalId + " .save").click(function () {
                var objlist = $("#" + modalId + " #table").datagrid("getChecked");
                for (var data in objlist) {
                    resultData.push(objlist[data]);
                }
                if (dialog.params.equipment == "2001") {
                    dialog.callBack(dialog.target, checkData);
                    dialog.modal('destroy');
                } else if (dialog.params.multiSelect === true) {
                    var allSelectRows = [];
                    for (var key in checkData) {
                        if (checkData[key]) allSelectRows.push(checkData[key]);
                    }
                    dialog.callBack(dialog.target, allSelectRows);
                    dialog.modal('destroy');
                } else {
                    dialog.callBack(dialog.target, resultData);
                    dialog.modal('destroy');
                }
            });
        });

        function search() {
            $("#" + modalId + " #table").datagrid("reload");
        }

        function reset() {
            $("#" + modalId + " #table").datagrid("removeFilterRule");
            $("#" + modalId + " #table").datagrid("options").queryParams.orgLevelCode = "";
            $("#" + modalId + " #table").datagrid("doFilter");
            // search();
        }

        function setWhere(dialog) {
            var params = dialog.params;
            if (params) {
                for (var key in params) {
                    $("#" + modalId + " #table").datagrid("options").queryParams.key = params[key];
                    // if ($("#" + modalId + " #searchFormDialog input[name='" + key + "']").length > 0) {
                    //     $("#" + modalId + " #searchFormDialog input[name='" + key + "']").val(params[key]);
                    // } else {
                    //     $("#" + modalId + " #searchFormDailog").append('<input type="hidden" name="' + key + '" value="' + params[key] + '" />');
                    // }
                }
            }
        }

        function btn_NoColumn(value, row, index) {
            if ("暂无数据" != row.receiveDate) {
                return index + 1;
            }

        }

    </script>
</div>
